
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;

public partial class admin_shipment_cost_edit : SiteAdminBasePage
{

    public int _shipmentCostId;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            uccResponseMessage.Visible = false;
            SetQueryValue();
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        catch {
            uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostGeneralFailed);
            uccResponseMessage.Visible = true;
        }
    }

    private void SetQueryValue()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
        {
            int.TryParse(Request.QueryString["cid"], out _shipmentCostId);
        }
    }
    private void LoadData()
    {
        ShipmentCost shipmentCost = ShipmentCost.Load(_shipmentCostId);
        this.BindData(shipmentCost);
    }
    protected void Update_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Page.Validate();
            if (Page.IsValid)
            {
                double weightRangeFrom;
                double weightRangeTo;
                double shipmentCostStandard;
                double shipmentCostExpress;
                double shipmentCostOvernight;
                string weightUnit;
                bool isActive;
                double.TryParse(tbxWeightRangeFrom.Text.Trim(), out weightRangeFrom);
                double.TryParse(tbxWeightRangeTo.Text.Trim(), out weightRangeTo);
                weightUnit = "KG";

                int shipmentCostId = 0;
                if (!string.IsNullOrEmpty(Request.QueryString["cid"]) &&
                    int.TryParse(Request.QueryString["cid"], out shipmentCostId))
                {
                    if (!ShipmentCost.IsWeightRangeValid(weightRangeFrom, weightUnit, shipmentCostId))
                    {
                        uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostDuplicateWeightRangeFrom);
                        uccResponseMessage.Visible = true;
                    }
                    else if (!ShipmentCost.IsWeightRangeValid(weightRangeTo, weightUnit, shipmentCostId))
                    {
                        uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostDuplicateWeightRangeTo);
                        uccResponseMessage.Visible = true;
                    }
                    else
                    {

                        ShipmentCost shipmentCost = ShipmentCost.Load(shipmentCostId);

                        double.TryParse(tbxStandardShippingCost.Text.Trim(), out shipmentCostStandard);
                        double.TryParse(tbxExpressShippingCost.Text.Trim(), out shipmentCostExpress);
                        double.TryParse(tbxOvernightShippingCost.Text.Trim(), out shipmentCostOvernight);
                        isActive = cbxIsActive.Checked;

                        shipmentCost.WeightRangeFrom = weightRangeFrom;
                        shipmentCost.WeightRangeTo = weightRangeTo;
                        shipmentCost.WeightUnit = weightUnit;
                        shipmentCost.ShippingCostStandard = shipmentCostStandard;
                        shipmentCost.ShippingCostExpress = shipmentCostExpress;
                        shipmentCost.ShippingCostOvernight = shipmentCostOvernight;
                        shipmentCost.IsActive = isActive;
                        shipmentCost.Update();
                        uccResponseMessage.Visible = true;
                        uccResponseMessage.SetSuccessMessage(AutoGearCommonMessage.ShipmentCostUpdateSuccessful);
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostUpdateFailed);
            uccResponseMessage.Visible = true;
        }
    }

    private void BindData(ShipmentCost shipmentCost)
    {
        if (shipmentCost != null)
        {
            tbxWeightRangeFrom.Text = shipmentCost.WeightRangeFrom.ToString();
            tbxWeightRangeTo.Text = shipmentCost.WeightRangeTo.ToString();
            tbxStandardShippingCost.Text = shipmentCost.ShippingCostStandard.ToString();
            tbxExpressShippingCost.Text = shipmentCost.ShippingCostExpress.ToString();
            tbxOvernightShippingCost.Text = shipmentCost.ShippingCostOvernight.ToString();
            cbxIsActive.Checked = shipmentCost.IsActive;
        }

    }

}


